﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKExcelFormTool.Utilities;

namespace YJKExcelFormTool.Kits
{
	public class DrawingPlotter
	{
		public DrawingPlotter(System.Windows.Forms.Control control, Graphics graphics, List<CurveInfo> curves, List<TextNoteInfo> textNotes, string fontName, double fontHeight)
		{
			this.m_Control = control;
			this.m_Graphics = graphics;
			this.m_Curves = curves;
			this.m_TextNotes = textNotes;
			this.m_FontName = fontName;
			this.m_FontHeight = fontHeight;
		}

		public void Draw()
		{
			if (this.m_Curves.Count < 1)
			{
				return;
			}
			this.m_Graphics.SmoothingMode = SmoothingMode.HighQuality;
			this.m_Graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
			this.m_Graphics.Clear(System.Drawing.Color.White);
			Size size = this.m_Control.Size;
			Size size2 = new Size((int)(0.9 * (double)size.Width), (int)(0.9 * (double)size.Height));
			Transform trans = this.ComputerTransMatrix(size2);
			foreach (CurveInfo curveInfo in this.m_Curves)
			{
				Pen penWithCurveInfo = this.GetPenWithCurveInfo(curveInfo.Color, curveInfo.LineWidth);
				Line line = curveInfo.CurveObj as Line;
				if (null != line)
				{
					this.DrawLine(penWithCurveInfo, line, trans);
				}
				else
				{
					Arc arc = curveInfo.CurveObj as Arc;
					if (null != arc)
					{
						this.DrawArc(penWithCurveInfo, arc, trans);
					}
				}
			}
			foreach (TextNoteInfo textNoteInfo in this.m_TextNotes)
			{
				this.DrawText(textNoteInfo, trans);
			}
		}

		protected Pen GetPenWithCurveInfo(PenColor penColor, float lineWidth)
		{
			return new Pen(this.PenColorToColor(penColor), lineWidth);
		}

		protected System.Drawing.Color PenColorToColor(PenColor penColor)
		{
			System.Drawing.Color white = System.Drawing.Color.White;
			switch (penColor)
			{
			case PenColor.PC_Red:
				return System.Drawing.Color.Red;
			case PenColor.PC_Blue:
				return System.Drawing.Color.Blue;
			case PenColor.PC_Green:
				return System.Drawing.Color.Green;
			case PenColor.PC_Gray:
				return System.Drawing.Color.Gray;
			case PenColor.PC_Yellow:
				return System.Drawing.Color.Yellow;
			case PenColor.PC_Black:
				return System.Drawing.Color.Black;
			}
			return System.Drawing.Color.White;
		}

		private Transform ComputerTransMatrix(Size size)
		{
			Transform identity = Transform.Identity;
			identity.Origin = XYZ.Zero;
			identity.BasisX = XYZ.BasisX;
			identity.BasisY = -XYZ.BasisY;
			identity.BasisZ = XYZ.BasisZ;
			BoundingBoxUV boundingBox = this.GetBoundingBox();
			UV max = boundingBox.Max;
			UV min = boundingBox.Min;
			double num = (double)size.Width / (max.U - min.U);
			double num2 = (double)size.Height / (max.V - min.V);
			double num3 = (num <= num2) ? num : num2;
			Transform transform = Transform.Identity;
			transform = transform.ScaleBasis(num3);
			Transform transform2 = RevitVersionFuncs.CreateTranslation(new XYZ((min.U + max.U) / 2.0, (min.V + max.V) / 2.0, 0.0));
			return Transform.Identity.Multiply(identity.Inverse).Multiply(transform).Multiply(transform2.Inverse);
		}

		protected BoundingBoxUV GetBoundingBox()
		{
			BoundingBoxUV boundingBoxUV = null;
			foreach (CurveInfo curveInfo in this.m_Curves)
			{
				Line line = curveInfo.CurveObj as Line;
				if (null != line)
				{
					if (boundingBoxUV == null)
					{
						boundingBoxUV = Geometry.CalculateBoundingBox2D(line);
					}
					else
					{
						BoundingBoxUV box = Geometry.CalculateBoundingBox2D(line);
						boundingBoxUV = Geometry.BoundingBoxesMerge(boundingBoxUV, box);
					}
				}
				else
				{
					Arc arc = curveInfo.CurveObj as Arc;
					if (null != arc)
					{
						if (boundingBoxUV == null)
						{
							boundingBoxUV = Geometry.CalculateBoundingBox2D(arc);
						}
						else
						{
							BoundingBoxUV box2 = Geometry.CalculateBoundingBox2D(arc);
							boundingBoxUV = Geometry.BoundingBoxesMerge(boundingBoxUV, box2);
						}
					}
				}
			}
			return boundingBoxUV;
		}

		protected void DrawLine(Pen pen, Line line, Transform trans)
		{
			XYZ xyz = line.GetEndPoint(0);
			XYZ xyz2 = line.GetEndPoint(1);
			xyz = Geometry.TransformPoint(xyz, trans);
			xyz2 = Geometry.TransformPoint(xyz2, trans);
			PointF pt = new PointF((float)xyz.X, (float)xyz.Y);
			PointF pt2 = new PointF((float)xyz2.X, (float)xyz2.Y);
			this.m_Graphics.DrawLine(pen, pt, pt2);
		}

		protected void DrawArc(Pen pen, Arc arc, Transform trans)
		{
			arc.GetEndPoint(0);
			arc.GetEndPoint(1);
			XYZ center = arc.Center;
			float num = (float)(arc.Radius * trans.Scale);
			XYZ normal = arc.Normal;
			double num2 = 0.0;
			double num3 = 0.0;
			Geometry.GetArcAngles(arc, ref num2, ref num3);
			float num4 = 6.28318548f - (float)num3;
			float num5 = 6.28318548f - (float)num2;
			if (num4 > num5)
			{
				num5 += 6.28318548f;
			}
			float num6 = num5 - num4;
			num4 = num4 * 180f / 3.14159274f;
			num6 = num6 * 180f / 3.14159274f;
			XYZ xyz = Geometry.TransformPoint(center, trans);
			float x = (float)(xyz.X - (double)num);
			float y = (float)(xyz.Y - (double)num);
			float num7 = num * 2f;
			this.m_Graphics.DrawArc(pen, x, y, num7, num7, num4, num6);
		}

		protected void DrawText(TextNoteInfo textNoteInfo, Transform trans)
		{
			if (textNoteInfo.m_Text.CompareTo("") == 0)
			{
				return;
			}
			float emSize = (float)(this.m_FontHeight * trans.Scale);
			Font font = new Font(this.m_FontName, emSize);
			SolidBrush brush = new SolidBrush(this.PenColorToColor(textNoteInfo.m_Color));
			XYZ xyz = Geometry.TransformPoint(textNoteInfo.m_Center, trans);
			double num = textNoteInfo.m_BoundingBoxWidth * trans.Scale;
			double num2 = textNoteInfo.m_BoundingBoxHeight * trans.Scale;
			double num3 = xyz.X - num / 2.0;
			double num4 = xyz.Y - num2 / 2.0;
			System.Drawing.Rectangle r = new System.Drawing.Rectangle((int)num3, (int)num4, (int)num, (int)num2);
			StringFormat stringFormat = new StringFormat();
			stringFormat.LineAlignment = StringAlignment.Center;
			stringFormat.Alignment = StringAlignment.Center;
			this.m_Graphics.DrawString(textNoteInfo.m_Text, font, brush, r, stringFormat);
		}

		private List<CurveInfo> m_Curves = new List<CurveInfo>();

		private List<TextNoteInfo> m_TextNotes = new List<TextNoteInfo>();

		private Graphics m_Graphics;

		private System.Windows.Forms.Control m_Control;

		private string m_FontName = "宋体";

		private double m_FontHeight = 3.5;
	}
}
